package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.QiNiuUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: ZPENG
 * @Date: 2020/11/26 20:15
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    //日志
    private static final Logger log = LoggerFactory.getLogger(SetmealController.class);

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;
    /**
     * 套餐图片上传
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){

        //获取原本图片的名称,截取到后缀名
        String originalFilename = imgFile.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //生成唯一的名称,拼接后缀名
        String imgName = UUID.randomUUID().toString() + suffix;

        try {
            //调用7牛 QiNiuUtils 上传
            QiNiuUtils.uploadViaByte(imgFile.getBytes(),imgName);

            //成功就返回图片名和域名给页面
            //{
            //   imgName:图片名
            //   domain: 域名
            //}

            //进行封装
            Map<String, String> resultMap = new HashMap<String, String>();
            resultMap.put("imgName",imgName);
            resultMap.put("domain",QiNiuUtils.DOMAIN);

            return new Result(true, MessageConstant.UPLOAD_SUCCESS,resultMap);

        } catch (IOException e) {
            //e.printStackTrace();
            log.error("上传文件失败!!!",e);//日志记录
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    /**
     * 添加套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        //调用业务服务添加
       Integer setmealId = setmealService.add(setmeal,checkgroupIds);
       //生成静态页面
       Jedis jedis = jedisPool.getResource();
       String key = "setmeal:static:html";
       jedis.sadd(key,setmealId+"|1|" + System.currentTimeMillis());
        jedis.close();
        //返回结果给页面
        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        //QueryPageBean 是个实体类 和前端pagination 对应

        //调用业务分页查询
       PageResult<Setmeal> pageResult = setmealService.findPage(queryPageBean);
       //返回结果
       return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,pageResult);
    }

    /**
     * 编辑  通过id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id){
        //调用服务查询
       Setmeal setmeal = setmealService.findById(id);
        //前端要显示图片需要全路径
        //封装到map中,解决图片路径问题
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("setmeal",setmeal);//formData 查询到的套餐信息
        resultMap.put("domain",QiNiuUtils.DOMAIN);//domain

        return new Result(true,MessageConstant.QUERY_SETMEAL_SUCCESS,resultMap);
    }

    /**
     * 通过id查询选中的检查组ids
     * @param id
     * @return
     */
    @GetMapping("findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(int id){
      List<Integer> checkgroupIds = setmealService.findCheckgroupIdsBySetmealId(id);
      return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkgroupIds);
    }
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        //调用服务更新
        //生成静态页面
        Jedis jedis = jedisPool.getResource();
        String key = "setmeal:static:html";
        jedis.sadd(key,setmeal.getId()+"|1|" + System.currentTimeMillis());
        jedis.close();
        setmealService.update(setmeal,checkgroupIds);
          return new Result(true,"更新套餐成功");
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id){
        //调用服务删除
        setmealService.deleById(id);
        //生成静态页面
        Jedis jedis = jedisPool.getResource();
        String key = "setmeal:static:html";
        jedis.sadd(key,id+"|0|" + System.currentTimeMillis());
        jedis.close();
        return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
